#!/bin/bash

daemonCmd() {
  INIT_SYSTEM=`cat /proc/1/comm 2>/dev/null`
  if test "$INIT_SYSTEM" = "systemd"
  then
     eval "systemctl $1 rstudio-server.service"
  elif test -e /etc/init/rstudio-server.conf
  then
     eval "initctl $1 rstudio-server"
  else
     eval "/etc/init.d/rstudio-server $1"
  fi
  return $?
}

testConfig() {
  `${CMAKE_INSTALL_PREFIX}/bin/rserver --test-config`
}

verifyInstallation() {
  ${CMAKE_INSTALL_PREFIX}/bin/rserver --verify-installation=1 --server-daemonize=0 $1 $2 $3 $4 $5 $6 $7 $8
}

isServerRunning() {
 pgrep -x rserver &> /dev/null
}

case "$1" in

    status)
        daemonCmd "status"
        ;;

    start)
        testConfig || exit $?
        daemonCmd "start"
        ;;

    stop)
        daemonCmd "stop"
        ;;

    restart)
        testConfig || exit $?
        daemonCmd "restart"
        ;;

    test-config)
        testConfig
        ;;

    verify-installation)
        if isServerRunning ; then
           echo "Server is running and must be stopped before running verify-installation"
           exit 1
        fi

        # we call stop here even though the service should already be stopped
        # just in case (we don't want the service running when invoking verify-installation)
        daemonCmd "stop" 2>/dev/null

        verifyInstallation $2 $3 $4 $5 $6 $7 $8 $9
        ;;

    suspend-session)
        if [ -n "$2" ]; then
           kill -s USR1 $3 $4 $5 $6 $7 $8 $9 $2
        else
           argType="PID"
           echo "Must specify $argType of session to suspend"
           exit 1
        fi
        ;;

    suspend-all)
        if (pidof rworkspaces >/dev/null); then killall -s USR1 $2 $3 $4 $5 $6 $7 $8 $9 rworkspaces; fi
        if (pidof rsession >/dev/null); then killall -s USR1 $2 $3 $4 $5 $6 $7 $8 $9 rsession; fi
        ;;

    force-suspend-session)
        if [ -n "$2" ]; then
           kill -s USR2 $3 $4 $5 $6 $7 $8 $9 $2
        else
           argType="PID"
           echo "Must specify $argType of session to force suspend"
           exit 1
        fi
        ;;

    force-suspend-all)
        if (pidof rworkspaces >/dev/null); then killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9 rworkspaces; fi
        if (pidof rsession >/dev/null); then killall -s USR2 $2 $3 $4 $5 $6 $7 $8 $9 rsession; fi
        ;;

    kill-session)
        if [ -n "$2" ]; then
           kill -s KILL $3 $4 $5 $6 $7 $8 $9 $2
        else
           argType="PID"
           echo "Must specify $argType of session to kill"
           exit 1
        fi
        ;;

    kill-all)
        if (pidof rworkspaces >/dev/null); then killall -s KILL $2 $3 $4 $5 $6 $7 $8 $9 rworkspaces; fi
        if (pidof rsession >/dev/null); then killall -s KILL $2 $3 $4 $5 $6 $7 $8 $9 rsession; fi
        ;;

    offline)
        mkdir -p /var/lib/rstudio-server
        touch /var/lib/rstudio-server/offline
        daemonCmd "restart"
        ;;

    online)
        mkdir -p /var/lib/rstudio-server
        rm -f /var/lib/rstudio-server/offline
        daemonCmd "restart"
        ;;

    active-sessions)
        ps opid,cputime,args -C "rsession"
        ;;

    version)
        echo "${CPACK_PACKAGE_VERSION} (${RSTUDIO_RELEASE_NAME}) for ${RSTUDIO_PACKAGE_OS}"
        ;;

    license-manager)
        echo "This open-source edition of RStudio Server does not require activation. Download and install RStudio Server Pro to use a licensed version of RStudio Server."
        exit 1
        ;;

    *)
        echo $"Usage: rstudio-server {status|start|stop|restart|test-config|verify-installation|suspend-session|suspend-all|force-suspend-session|force-suspend-all|kill-session|kill-all|offline|online|active-sessions|version}"
        exit 2
esac


